import random
from selenium import webdriver
from selenium.webdriver.support.select import Select

class BoxDriver:

    def __init__(self,broswer='Chrome') -> None:
        if broswer == 'Chrome':
            self.driver = webdriver.Chrome()
        elif broswer == 'Firefox':
            self.driver = webdriver.Firefox()
        elif broswer == 'Edge':
            self.driver = webdriver.Edge()
        elif broswer == 'Opera':
            self.driver = webdriver.Opera()
        elif broswer == 'Safari':
            self.driver = webdriver.Safari()
        elif broswer == 'Ie':
            self.driver = webdriver.Ie()

    def get(self,url):
        """  
        打开指定的页面
        url     页面地址
        """
        self.driver.get(url)

    def maximize_window(self):
        """
        窗口最大化
        """
        self.driver.maximize_window()

    def implicitly_wait(self,seconds):
        """
        隐式等待
        seconds     最大等待时间，单位是秒
        """
        self.driver.implicitly_wait(seconds)

    def input(self,by,value,text):
        """  
        向元素中写入内容
        by      定位方式
        value   定位值
        text    写入的内容
        """
        self.driver.find_element(by,value).send_keys(text)

    def click(self,by,value):
        """  
        单击元素
        by      定位方式
        value   定位值
        """
        self.driver.find_element(by,value).click()

    def switch_to_frame(self,by,value):
        """  
        进入iframe元素
        """
        iframe = self.driver.find_element(by,value)
        self.driver.switch_to.frame(iframe)

    def select_by_index(self,by,by_value,index):
        """  
        根据index进行选择
        by      定位方式
        by_value   定位值   
        index      下标    
        """
        select = self.driver.find_element(by,by_value)
        depts = Select(select)
        depts.select_by_index(index)    # 根据下标选择，下标从0开始

    def select_by_value(self,by,by_value,value):
        """  
        根据value进行选择
        by      定位方式
        by_value   定位值   
        index      下标    
        """
        select = self.driver.find_element(by,by_value)
        depts = Select(select)
        depts.select_by_value(value)    # 根据下标选择，下标从0开始

    def select_by_visible_text(self,by,by_value,visible_text):
        """  
        根据value进行选择
        by      定位方式
        by_value   定位值   
        index      下标    
        """
        select = self.driver.find_element(by,by_value)
        depts = Select(select)
        depts.select_by_visible_text(visible_text)    # 根据下标选择，下标从0开始

    def find_elements(self,by,value):
        """  
        定位读个元素
        by      定位方式
        by_value   定位值   
        """
        return self.driver.find_elements(by,value)

    def find_element_by_partial_link_text(self,by,value):
        """
        模糊匹配ahref连接中的文本
        by      定位方式
        by_value   定位值
        """
        return self.driver.find_element_by_partial_link_text(by,value)

    def find_element_by_link_text(self,by,value):
        """
        精确匹配ahref连接中的文本
        by      定位方式
        by_value   定位值
        """
        return self.driver.find_element_by_link_text(by,value)

    def close(self):
        """  
        关闭当前窗口
        """
        self.driver.close()


class BasePage:

    def __init__(self,driver:BoxDriver) -> None:
        self.driver = driver

if __name__ == '__main__':
    BoxDriver()